package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.TkmSysOrgService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @ClassName:TkmSysOrgServiceController
 * @功能：机构管理
 * @author：wlei
 * @日期：2022/8/5-04:11
 **/
@RestController
@Slf4j
@RequestMapping("/v1/orgsetting/")
public class TkmSysOrgServiceController extends BaseController {

    @Autowired
    private TkmSysOrgService tkmSysOrgService;

    @GetMapping("listAllData")
    @ApiOperation("获取所有的机构数据列表")
    public JsonObj listAllData() {
        return tkmSysOrgService.listAllData(getPageData());
    }

    @GetMapping("getAllProvince")
    @ApiOperation("获取所有的省份数据列表")
    public JsonObj getAllProvince() {
        return tkmSysOrgService.getAllProvince();
    }

    @GetMapping("getAllProvinceCity")
    @ApiOperation("获取所有的省份数据列表")
    public JsonObj getAllProvinceCity() {
        PageData pageData = getPageData();
        String parentRegionCode = (String) pageData.get("parentRegionCode");
        String user = (String) pageData.get("user");
        return tkmSysOrgService.getAllProvinceCity(parentRegionCode, user);
    }

    @GetMapping("getDistrictAndStreet")
    @ApiOperation("获取所有的区街道信息")
    public JsonObj getDistrictAndStreet() {
        PageData pageData = getPageData();
        String parentRegionCode = (String) pageData.get("parentRegionCode");
        return tkmSysOrgService.getDistrictAndStreet(parentRegionCode);
    }

    @PostMapping("exportOrgData")
    @ApiOperation("导出机构数据")
    public void exportOrgData(HttpServletResponse response) {
        PageData pageData = getPageData();

        // 查询出可以用来导出的数据源
        JsonObj jsonObj = tkmSysOrgService.exportOrgData(pageData);
        List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) jsonObj
            .getData()
            .get("data");

        // 组装表头
        List<String> headList = Arrays.asList("机构名称",
                                              "隶属省份",
                                              "隶属城市",
                                              "隶属区县",
                                              "机构编码",
                                              "详细地址",
                                              "创建时间");

        try {
            // 导出
            ExcelUtils.exportExcel(headList, dataList, "机构数据汇总", response);
        } catch (Exception e) {
        }
    }
}
